package com.itdianzi.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itdianzi.dao.cargo.ContractDao;
import com.itdianzi.dao.cargo.ExtCproductDao;
import com.itdianzi.domain.cargo.Contract;
import com.itdianzi.domain.cargo.ExtCproduct;
import com.itdianzi.domain.cargo.ExtCproductExample;
import com.itdianzi.service.cargo.ExtCproductService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ExtCproductServiceImpl implements ExtCproductService{
    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;

    @Override
    public void save(ExtCproduct extCproduct) {
        //附件的保存
        //1.附件表
        //1.1 计算附件的总金额
        Double amount = 0d;
        if (extCproduct.getPrice() != null && extCproduct.getCnumber() != null) {
            amount = extCproduct.getPrice() * extCproduct.getCnumber();
        }
        //1.2 将附件总金额 赋值给附件对象
        extCproduct.setAmount(amount);

        //1.3 保存附件对象
        extCproduct.setId(UUID.randomUUID().toString());
        extCproductDao.insertSelective(extCproduct);
        //2.合同表
        //2.1 先根据附件对象查询到合同对象
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());

        //2.2 计算合同总金额
        contract.setTotalAmount(contract.getTotalAmount() + amount);

        //2.3 附件的种类数据
        contract.setExtNum(contract.getExtNum() + 1);

        //2.4 修改合同对象即可
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override
    public void update(ExtCproduct extCproduct) {
        //修改附件
        //1.附件表
        //先查询以前附件的对象 - 要以前的总金额 方便后续的计算
        ExtCproduct oldExtCproduct = extCproductDao.selectByPrimaryKey(extCproduct.getId());

        //1.1 计算总金额
        Double amount = 0d;
        if (extCproduct.getPrice() != null && extCproduct.getCnumber() != null) {
            amount = extCproduct.getPrice() * extCproduct.getCnumber();
        }
        //1.2 修改附件
        extCproduct.setAmount(amount);
        extCproductDao.updateByPrimaryKeySelective(extCproduct);
        //2.合同表
        //2.1 根据附件对象获得合同的对象
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //2.2 计算总金额
        contract.setTotalAmount(contract.getTotalAmount() - oldExtCproduct.getAmount() + amount);
        //2.3 修改合同即可
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {
        //删除附件
        //1.附件表
        //1.1 先获得附件的对象
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        //1.2 删除附件即可
        extCproductDao.deleteByPrimaryKey(id);
        //2.合同表
        //2.1 根据已经查询附件对象 查询合同对象
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //2.2 计算总金额
        contract.setTotalAmount(contract.getTotalAmount() - extCproduct.getAmount());
        //2.3 附件的种类数量
        contract.setExtNum(contract.getExtNum()-1);
        //2.4 修改合同表
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExtCproductExample example, int page, int size) {
        PageHelper.startPage(page, size);
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(example);

        return new PageInfo(extCproducts);
    }
}
